package com.quickdv.activity.fragment;

import android.app.Activity;
import android.app.ProgressDialog;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.quickdv.helper.ActivityImp;
import com.quickdv.helper.imp.ActionInfo;
import com.quickdv.until.LogHelper;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link BaseFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link BaseFragment#} factory method to
 * create an instance of this fragment.
 */
public abstract class BaseFragment extends Fragment implements ActionInfo {

    protected OnFragmentInteractionListener mListener;

    protected Activity activity;

    protected static String Tag;

    protected View root;

    private ActivityImp help;

    protected String mParam1;

    protected static final String ARG_PARAM1 = "param1";

//    protected NetWorkImp network;
    protected ProgressDialog dialog;

    protected LogHelper log;

    public boolean attach = false;

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        attach = isVisibleToUser;
        super.setUserVisibleHint(isVisibleToUser);

    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;
        log = LogHelper.getInstance();
        help = new ActivityImp(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root = inflater.inflate(getLayoutViewID(), container, false);
        initData();
        initView();
        setCLickListener();
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
        setRetainInstance(true);
        return root;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public View getRootView() {
        return root;
    }

    public <T extends View> T findView(int id) {
        return (T) help.findView(root, id);
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

    public String getFragTag() {
        return Tag;
    }

    public void startActivity(String action) {
        help.startActivity(action);
    }

    public void startActivity(String action, Bundle bundle) {
        help.startActivity(action, bundle);
    }

    protected abstract void initView();

    protected abstract int getLayoutViewID();

    protected void initData() {}

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        attach=false;
    }
}
